/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MODEL.DAO;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import MODEL.POJO.*;
import java.util.List;

/**
 *
 * @author samph
 */
public class UsersDao {
     public static List<Users> LayDanhSachNguoiDung(){
        List<Users> ds = null; 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        try { 
            String hql = "from Users"; 
            Query query = session.createQuery(hql); 
            ds = query.list(); 
        } catch (HibernateException ex) { 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return ds; 
    }
     public static List<Users> checkExistIdentifyCard(int identify){
        List<Users> ds = null; 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        try { 
            String hql = "from Users u where u.userIdentityCard = :identify"; 
            Query query = session.createQuery(hql); 
            query.setParameter("identify", identify);
            ds = query.list(); 
        } catch (HibernateException ex) { 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return ds; 
    }
     public static boolean addUser(Users u) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(u);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex.getMessages());
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }
}
